/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.web30.mobile.view.news;

import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author xiazy
 */
public class Feed {
    private SyndFeed _feed = null;
    private ArrayList<Entry> _entries = null;

    public SyndFeed getFeed() {
        return _feed;
    }

    public void setFeed(SyndFeed _feed) {
        this._feed = _feed;
    }

    public Feed(URL url) {
        try {
            SyndFeedInput input = new SyndFeedInput();
            InputStreamReader stream = new InputStreamReader(url.openStream(),"UTF-8");                        
            this._feed = input.build(stream);
        } catch (IOException ex) {
            Logger.getLogger(Feed.class.getName()).log(Level.SEVERE, null, ex);
            this._feed = null;
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Feed.class.getName()).log(Level.SEVERE, null, ex);
            this._feed = null;
        } catch (FeedException ex) {
            Logger.getLogger(Feed.class.getName()).log(Level.SEVERE, null, ex);
            this._feed = null;
        }
    }
    public List getEntries(){
        if(this.isValid() == false){
            return null;
        }
        if(this._entries == null){
            this._entries = new ArrayList<Entry>(this._feed.getEntries().size());
            for(int i = 0 ; i < this._feed.getEntries().size(); i++){
                Entry temp = new Entry((SyndEntry)this._feed.getEntries().get(i),i);
                this._entries.add(temp);
                System.out.println(i);
            }
        }
        return this._entries;
    }
    public String getTitle() {
        return this._feed.getTitle();
    }
    public boolean isValid() {
        if(this._feed == null)
            return false;
        return true;
    }
}
